package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.reggie.common.MyBaseContext;
import com.example.reggie.common.R;
import com.example.reggie.domain.ShoppingCart;
import com.example.reggie.service.ShoppingCartService;
import com.example.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController extends BaseController{

    @Autowired
    private ShoppingCartService shoppingCartService;


    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
        shoppingCartLambdaQueryWrapper.orderByDesc(ShoppingCart::getCreateTime);

        return R.success(shoppingCartService.list(shoppingCartLambdaQueryWrapper));
    }

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

        Long userId = (Long) session.getAttribute("user");
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        shoppingCart.setUserId(userId);

        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(dishId!=null,ShoppingCart::getDishId,dishId);
        shoppingCartLambdaQueryWrapper.eq(setmealId!=null,ShoppingCart::getSetmealId,setmealId);
        shoppingCartLambdaQueryWrapper.eq(userId!=null,ShoppingCart::getUserId,userId);

        ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);

        if(ObjectUtils.isEmpty(one)){
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
        Integer number = one.getNumber();
        one.setNumber(number+1);

        shoppingCartService.updateById(one);

        return R.success(one);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long userId = (Long) session.getAttribute("user");
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(dishId!=null,ShoppingCart::getDishId,dishId);
        shoppingCartLambdaQueryWrapper.eq(setmealId!=null,ShoppingCart::getSetmealId,setmealId);
        shoppingCartLambdaQueryWrapper.eq(userId!=null,ShoppingCart::getUserId,userId);

        ShoppingCart shoppingCartOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        Integer number = shoppingCartOne.getNumber();
        if(number>1){
            shoppingCartOne.setNumber(number-1);
            shoppingCartService.updateById(shoppingCartOne);
            return R.success(shoppingCartOne);
        }

        if(number == 1){
            shoppingCartService.removeById(shoppingCartOne);
            shoppingCartOne.setNumber(null);
            return R.success(shoppingCartOne);
        }

        return R.error("数量减异常！");
    }

    @DeleteMapping("/clean")
    public R<String> clean(){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(userId!=null,ShoppingCart::getUserId, userId);
        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        return R.success("清空购物车成功！");
    }



}
